/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.bl.CategoriaBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Categoria;

/**
 *
 * @author Dhavis
 */
@ManagedBean
@SessionScoped
public class CathegoryBean {

    /**
     * Creates a new instance of CathegoryBean
     */
     @EJB
    private CategoriaBlLocal categoriaBl;
    
    //--------------------------Variables (propiedades)---------------------
    private List<Categoria> lista;
    private Categoria category;
    private String accion;

    //--------------------------Constructor---------------------------------
    /**
     * Creates a new instance of CategoryBean
     */
    public CathegoryBean() {
    }
    
    //--------------------------metodos GET y SET---------------------------

    public List<Categoria> getLista() {
        return lista;
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }

    
    
    //--------------------------Acciones------------------------------------
    
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
            categoriaBl.register(category);
        }else{
            if(accion.equalsIgnoreCase("Editar")){
                categoriaBl.modify(category);
            }else{
                if(accion.equalsIgnoreCase("Eliminar")){
                    categoriaBl.delete(category);
                }
            }
        }
        accion = "";
        return "Categoria";
    }
    
    //--------------------------Listeners-----------------------------------
    public void prepararNuevo(ActionEvent evt){
        category = new Categoria();
        accion = "Nuevo";
    }
    
    public void prepararEditar(ActionEvent evt){
        System.out.println(evt.getComponent().getClientId());
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        System.out.println("El numero es "+ index);
        category = lista.get(index);
        accion = "Editar";
    }
    
    public void prepararEliminar(ActionEvent evt){
        System.out.println(evt.getComponent().getClientId());
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        System.out.println("El numero es "+ index);
        category = lista.get(index);
        accion = "Eliminar";
    }

   
}
